package com.magic.gre.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.gre.R;
import com.magic.gre.base.fragment.BaseMVPFragment;
import com.magic.gre.helper.Contract;
import com.magic.gre.helper.WXConfig;
import com.magic.gre.mvp.contract.SmsCodeContract;
import com.magic.gre.mvp.presenter.SmsCodePresenterImpl;
import com.magic.gre.ui.activity.CodeActivity;
import com.magic.gre.ui.activity.WebActivity;
import com.noname.lib_base_java.util.PathUtil;
import com.noname.lib_base_java.util.ToastUtil;
import com.noname.lib_base_java.util.ValidateUtil;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: Lmoumou
 * Date: 2019-01-10 12:27
 * 文件名: PhoneLoginFragment
 * 描述:
 */
public class PhoneLoginFragment extends BaseMVPFragment<SmsCodePresenterImpl> implements SmsCodeContract.View {

    @BindView(R.id.phone_edt)
    EditText phoneEdt;
    @BindView(R.id.agreement_tv)
    TextView agreementTv;
    @BindView(R.id.agreement_tv2)
    TextView agreementTv2;

    public static PhoneLoginFragment newInstance() {
        return new PhoneLoginFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_login_phone;
    }


    @Override
    protected SmsCodePresenterImpl creatPresenter() {
        return new SmsCodePresenterImpl(this);
    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    protected void netOverride() {

    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        super.initView(view, savedInstanceState);
    }

    @OnClick(value = {R.id.code_bt, R.id.agreement_tv,R.id.agreement_tv2,R.id.wx_login})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.code_bt:
                String phoneStr = String.valueOf(phoneEdt.getText());
                if (!ValidateUtil.Mobile(phoneStr)){
                    ToastUtil.getInstance().showNormal(mContext, "请输入正确的电话号码");
                    return;
                }
                if (!agreementTv.isSelected()){
                    ToastUtil.getInstance().showNormal(mContext, "您未同意《用户使用协议》");
                    return;
                }
                showLoadDialog();
                mPresenter.pSmsCode(phoneStr,2);
                break;
            case R.id.agreement_tv:
                agreementTv.setSelected(!agreementTv.isSelected());
                break;
            case R.id.agreement_tv2:
                WebActivity.startThis(mContext,"用户协议", PathUtil.urlPath(Contract.AGREEMENT_URL));
                break;
            case R.id.wx_login:
                loginWx();
                break;
        }
    }

    /**
     * 微信登陆
     */
    private void loginWx() {
        IWXAPI api= WXAPIFactory.createWXAPI(mContext, WXConfig.APP_ID_WX, true);

        api.registerApp(WXConfig.APP_ID_WX);
        SendAuth.Req req=new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = WXConfig.APP_STATE_WX;
        api.sendReq(req);

    }

    @Override
    public void vSmsCode(String code) {
        hideLoadDialog();
        ToastUtil.getInstance().showNormal(mContext, code);
        CodeActivity.startThis(mContext, CodeActivity.LOGIN, String.valueOf(phoneEdt.getText()),code);
    }
}
